/**
 * @project name:crm
 */
package com.yunbo.crm.interceptor;

import com.yunbo.crm.dao.UserMapper;
import com.yunbo.crm.exceptions.NoLoginException;
import com.yunbo.crm.utils.LoginUserUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author : yun&bo
 * @CreateDate : ==>2021/1/21 11:28
 * @ClassName : ==>
 * @Description : ==> 非法请求拦截器
 * 判断用户是否是登入状态，判断cookie是否有cookie对象
 * @Version
 */

@Component
public class NoLoginInterceptor extends HandlerInterceptorAdapter {
    @Resource
    private UserMapper userMapper;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Integer userId = LoginUserUtil.releaseUserIdFromCookie(request);
        //判断用户Id 是否为空，且用户对象存在
        if (null == userId || userMapper.selectByPrimaryKey(userId) == null) {
            throw new NoLoginException();
        }
        return super.preHandle(request, response, handler);
    }
}
